package com.cndw.kungfu.model.operation;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsFruit;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LangUtil;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.operation.domain.PlayerFruit;
import com.cndw.kungfu.model.operation.domain.TreeFruit;
import com.cndw.kungfu.model.sys.ChatMain;

@Service
public class OperationServer {

	public Map<Integer, PlayerFruit> initTreeFruit(long playerId) {
		Map<Integer, PlayerFruit> tmpMap = new HashMap<Integer, PlayerFruit>(3);
		int currentTime = TimeProcesser.getUnixTime();
		for (int i = 1; i <= 3; i++) {
			tmpMap.put(i, new PlayerFruit(i, currentTime));// 初始增加
		}
		return tmpMap;
	}

	public void treeFruitGrow(long playerId, Map<Integer, PlayerFruit> fruitMap) {
		Iterator<PlayerFruit> it = fruitMap.values().iterator();
		int currentTime = TimeProcesser.getUnixTime();
		while (it.hasNext()) {
			PlayerFruit playerFruit = (PlayerFruit) it.next();
			while (playerFruit.getSeason() < ConstantsFruit.fruitSeasonMax && playerFruit.getBegin() + ConstantsFruit.fruitCD <= currentTime) {
				fruitMature(playerId, playerFruit, false);
			}
		}
	}
	public void fruitMature(long playerId, PlayerFruit fruit, boolean useGold) {
		fruitMature(playerId, fruit, useGold,0);
	}

	public void fruitMature(long playerId, PlayerFruit fruit, boolean useGold,int targetLevel) {
		TreeFruit treeFruit = TreeFruit.getOne(fruit.getType(), fruit.getLevel());
		if (treeFruit == null) {
			fruit.setType(0);
			fruit.setLevel(0);
			fruit.setSeason(0);
			return;
		} else if (fruit.getSeason() >= ConstantsFruit.fruitSeasonMax) {
			return;
		}
		boolean down = NumberProcesser.isWinning(useGold ? treeFruit.getRateDownGold() : treeFruit.getRateDown());
		boolean up = NumberProcesser.isWinning(useGold ? treeFruit.getRateUpGold() : treeFruit.getRateUp());
		int currentLevel = fruit.getLevel();
		if (down) {
			currentLevel--;
		} else if (up) {
			currentLevel++;
		}
		if (targetLevel >0 ) {
			currentLevel = 5;
			fruit.setSeason(5);
			up = true;
			down = false;
		}
		currentLevel = NumberProcesser.between(currentLevel, 1, 5);
		if (fruit.getType() == 0) {
			fruit.setType(NumberProcesser.rateIndex(ConstantsFruit.fruitRateType));// 随机转类型
		}

		if (fruit.getType() == 0) {
			fruit.setType(NumberProcesser.mtRand(3));// 随机转类型
		}
		fruit.setLevel(currentLevel);
		TreeFruit treeFruitNew = TreeFruit.getOne(fruit.getType(), fruit.getLevel());
		if (fruit.getSeason() < 5) {
			fruit.setSeason(fruit.getSeason() + 1);
		}
		String content = LanguageLib.get(down ? "operation.grow.down" : up ? "operation.grow.up" : "operation.grow.nomal",
				LangUtil.makeHtml(treeFruit.getLevel() - 1, treeFruit.getName()), fruit.getSeason(),
				LangUtil.makeHtml(treeFruitNew.getLevel() - 1, treeFruitNew.getName()));
		if (useGold) {
			fruit.setBegin(TimeProcesser.getUnixTime());
			fruit.setFast(true);
		} else {
			fruit.setBegin(fruit.getBegin() + ConstantsFruit.fruitCD);
		}
		Context.getMessageServer().recordEvent(playerId, LanguageLib.get("operation.grow.title", fruit.getSeason()), content, Constants.eventTypeFruit);
		if (fruit.getLevel() >= 4) {
			Player player = Context.getPlayerServer().getByPlayerId(playerId);
			ChatMain.systemMessage(LanguageLib.get("operation.grow.chat", player.getPlayerName(),
					LangUtil.makeHtml(fruit.getLevel() - 1, treeFruitNew.getName())));
		}

	}

}
